package com.example.test2.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.test2.R;
import com.example.test2.db.entity.Clothes;
import com.example.test2.db.entity.ClothesInfo;

import java.util.ArrayList;
import java.util.List;


// TODO: 2024/5/29 搜索页面商品适配器
public class SearchClothesAdapter extends RecyclerView.Adapter<SearchClothesAdapter.ViewHolder> {

    private Context context;
    private List<Clothes> list = new ArrayList<>();

    public SearchClothesAdapter(Context context, List<Clothes> list) {
        this.context = context;
        this.list = list;
    }

    public void setList(List<Clothes> list) {
        this.list = list;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_clothes2, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        Clothes clothes = list.get(position);
        String name = clothes.getName();
        holder.tvName.setText(name);

        if (clothes.getUserAdd()){
            Bitmap bitmap= BitmapFactory.decodeFile(clothes.getPath());
            holder.ivIcon.setImageBitmap(bitmap);
        }else {
            holder.ivIcon.setImageResource(ClothesInfo.valueOf(name).getImage());
        }
        String price=clothes.getPrice();
        if (price.contains("/")){
            holder.tvPrice.setText(price.substring(0,price.indexOf("/")));
            holder.tvUnit.setText(price.substring(price.indexOf("/")));
        }else {
            holder.tvPrice.setText(price);
        }
        holder.tvLocation.setText(clothes.getAddress());

        holder.llMain.setOnClickListener(v -> {
            if (listener != null) {
                listener.onEdit(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private LinearLayout llMain;
        private ImageView ivIcon;
        private TextView tvName;
        private TextView tvPrice;
        private TextView tvLocation;
        private TextView tvUnit;


        public ViewHolder(View itemView) {
            super(itemView);
            llMain = itemView.findViewById(R.id.ll_main);
            tvUnit= itemView.findViewById(R.id.tv_unit);
            ivIcon = itemView.findViewById(R.id.iv_img);
            tvName = itemView.findViewById(R.id.tv_name);
            tvPrice = itemView.findViewById(R.id.tv_price);
            tvLocation = itemView.findViewById(R.id.tv_location);
        }
    }

    private OnClickListener listener;

    public void setListener(OnClickListener listener) {
        this.listener = listener;
    }

    public interface OnClickListener {
        void onEdit(int pos);
    }

}
